package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;


    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto) {
        return setmealService.saveSetMeal(setmealDto);
    }

    @GetMapping("/page")
    public R page(int page, int pageSize, String name) {
        return setmealService.pageSetmeal(page, pageSize, name);
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {

        return setmealService.getSetMealById(id);
    }

    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto) {

        return setmealService.updateSetMeal(setmealDto);
    }

    @PostMapping("/status/{id}")
    public R status(@PathVariable int id, Long[] ids) {
        List<Setmeal> list = new ArrayList<>();
        for (Long aLong : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(aLong);
            setmeal.setStatus(id);
            list.add(setmeal);
        }
        return setmealService.updateStatus(list);
    }
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){

        return setmealService.deleteSetMeal(ids);
    }
    @GetMapping("/list")
   // public R list(Long categoryId,int status){
    public R list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(null!=setmeal,Setmeal::getCategoryId,setmeal.getCategoryId()).eq(Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> setmeals = setmealService.list(lqw);
        if (null!=setmeals) {
            return R.success(setmeals);
        }
        return R.error("没查询到");
    }

    @GetMapping("/dish/{dishId}")
    public R dish(@PathVariable Long dishId){
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(null!= dishId,Dish::getId,dishId);
        List<Dish> list = dishService.list(lqw);
        if (null!=list) {
            return R.success(list);
        }
        return R.error("没查询到");
    }

}
